package com.example.todofinally;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextClock;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.todofinally.adaoter.MainAdapter;
import com.example.todofinally.sql.Data;
import com.example.todofinally.sql.SpHelper;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private List<Data> list;
    private EditText editText_date;
    private TextClock mTextClock;
    private ImageButton timeChoose_button;
    private final String TAG="MainActivity";
    private int year;
    private int month;
    private int day;
    private SpHelper spHelper;


    private CalendarView calendarView;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText_date=findViewById(R.id.add_date);
        mTextClock = findViewById(R.id.text_clock);
        // 设置12时制显示格式
        mTextClock.setFormat12Hour("EEEE, MMMM dd, yyyy h:mmaa");
        // 设置24时制显示格式
        mTextClock.setFormat24Hour("yyyy-MM-dd hh:mm, EEEE");
        FloatingActionButton FloatingActionButton = findViewById(R.id.FloatingActionButton);
        RecyclerView rv_home = findViewById(R.id.rv_home);
        rv_home.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        spHelper=new SpHelper(MainActivity.this);
        list = spHelper.query1();
        //System.out.println(list);
   /*     for (Data data : list) {
            Log.i("list11", ""+data.toString());
        }*/

        rv_home.setAdapter(new MainAdapter(MainActivity.this,list));

        calendarView = findViewById(R.id.calendarView);
        dot();
        pastDue();


    }


    public void dot(){
        Map<String, Calendar> map = new HashMap<>();
        for (Data data : list) {
            String date = data.getDate();
//            Log.i("date11", ""+data.getDate());

            if(date.length() >= 10) {
                year = Integer.parseInt(date.substring(0, 4));
                month = Integer.parseInt(date.substring(5, 7));
                day = Integer.parseInt(date.substring(8, 10));
            }
            Calendar calendar = new Calendar();
            calendar.setYear(year);
            calendar.setMonth(month);
            calendar.setDay(day);
            map.put(calendar.toString(),calendar);
            calendarView.setSchemeDate(map);
        }
    }



    @RequiresApi(api = Build.VERSION_CODES.O)
    public void pastDue(){
        LocalDateTime now = LocalDateTime.now();
        int nowYear = now.getYear();
        int nowMonth = now.getMonthValue();
        int nowDay = now.getDayOfMonth();
        String s1 = nowYear + nowMonth +  nowDay+"";
        long l1 = Long.valueOf(s1);

        for (Data data : list) {
            String date = data.getDate();

            year = Integer.parseInt(date.substring(0, 4));
            month = Integer.parseInt(date.substring(5, 7));
            day = Integer.parseInt(date.substring(8, 10));

            String s2 = year + month +  day+"";
            long l2 = Long.valueOf(s2);

            if(l1> l2) {
                spHelper.changeOverdue(data.id+"");
            }
        }
    }



    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onResume() {
        super.onResume();
        RecyclerView rv_home = findViewById(R.id.rv_home);
        rv_home.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        SpHelper spHelper = new SpHelper(MainActivity.this);
        list = spHelper.query1();
        rv_home.setAdapter(new MainAdapter(MainActivity.this,list));
        dot();
        pastDue();

    }

    public void floatClick(View view) {
        Intent FloatingActionButton = new Intent(this,edit_Activity.class);
        startActivity(FloatingActionButton);
    }





    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater=getMenuInflater();//获取菜单Inflater,MenuInflater加载menu布局文件
        inflater.inflate(R.menu.menu1,menu);//通过inflate获取菜单资源
        return true;
    }




    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.one1: {
                Intent intent = new Intent(this, ListActivity.class);
                startActivity(intent);
            }
                break;
            case R.id.one2:{
                Intent intent = new Intent(this,StatisticsActivity.class);
                startActivity(intent);
            }
                break;
            default:
                return true;
        }
        return true;
    }







}